package com.meitianhui.convenitenbuy.util;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.meitianhui.convenitenbuy.R;

/**
 * toast显示
 */

public class TipUtils {

    private static Toast mToast;

    private static Toast mViewToast;

    /**
     * 非阻塞试显示Toast,防止出现连续点击Toast时的显示问题
     */
    public static void toast(Context context, String text, int duration) {
        if (mToast == null) {
            mToast = Toast.makeText(context, text, duration);
        } else {
            mToast.setText(text);
            mToast.setDuration(duration);
        }
        mToast.show();
    }

    public static void showToast(Context context, String text) {
        toast(context, text, Toast.LENGTH_SHORT);
    }

    public static void showToastLong(Context context, String text) {
        toast(context, text, Toast.LENGTH_LONG);
    }

    /**
     * @param type 1代表成功 2代表失败 3代表普通提示
     */
    public static void showToast(Context context, String text, int type) {
        if (TextUtils.isEmpty(text)) {
            return;
        }
        if (mViewToast == null) {
            mViewToast = new Toast(context);
        }
        mViewToast.setDuration(Toast.LENGTH_SHORT);
        mViewToast.setGravity(Gravity.CENTER, 0, 0);
        View view = LayoutInflater.from(context).inflate(R.layout.toast_custom, null);
        TextView textView = (TextView) view.findViewById(R.id.tv_toast);
        textView.setText(text);
        if (type == 1) {
            ImageView imageView = (ImageView) view.findViewById(R.id.iv_toast);
            imageView.setBackgroundResource(R.mipmap.icon_tip_success);
        } else if (type == 3) {
            ImageView imageView = (ImageView) view.findViewById(R.id.iv_toast);
            imageView.setBackgroundResource(R.mipmap.icon_tip_smile);
        }
        mViewToast.setView(view);
        mViewToast.show();
    }
}
